package com.example.orderserver.controller;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.example.orderserver.entities.Order;
import com.example.orderserver.entities.OrderDto;
import com.example.orderserver.service.OrderService;
import com.example.server_engineer.controller.AbstractTypedController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;
import java.util.List;


@Slf4j
@RestController
@Tag(name = "订单控制器")
@RequestMapping(value = "order")
public class OrderController extends AbstractTypedController<Order, Integer> {
    // 字段注入 属性注入  Spring 3.0 提倡构造函数注入

    OrderController(OrderService orderService) {
        this.svcContext = orderService;
    }
    @Resource
    OrderService osvc;

    @Operation(summary = "用户下单")
    @PostMapping("/placeOrder")
    public Order placeOrder(String foodname,Long userid,Integer quantity,Integer daid) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        return osvc.placeOrder(userid,foodname, quantity, daid);
    }

    @Operation(summary = "测试流控")
    @GetMapping("/testflow")
//    @SentinelResource(value = "testFlow",blockHandler = "testFlowHandler")
    public String testFlow(){
        return "testflow";
    }

//    public String testFlowHandler(BlockException e){
//        return "请求次数太快,被流控了";
//    }

//    @Operation(summary = "查询已支付订单")
//    @GetMapping("/query/paid/by")
//    public List<OrderDto> queryPaidOrder(String userid){
//        return osvc.findOrder(userid,1);
//    }
//
//    @Operation(summary = "查询未支付订单")
//    @GetMapping("/query/unpaid/by")
//    public List<OrderDto> queryUnpaidOrder(String userid){
//        return osvc.findOrder(userid,0);
//    }
}
